<?php


namespace api\web\controller;


use api\web\model\NewsModel;
use cmf\controller\RestBaseController;

class NewsController extends RestBaseController
{
    public function index(){
        //搜索获取字段
        $key = input('key');
        $page = input('page/d','1');
        $pageSize = input('page_size/d','10');
        $where =[];
        //判断是否有值
        if($key){
            $where[] = ['author|title', 'like', '%' . $key . '%'];
        }
        //判断是否有值
        $list = NewsModel::field('id,title,author,image,abstract,look_num,create_time')
                ->order('list_order','DESC')
                ->where($where)
                ->page($page,$pageSize)
                ->select();

        $this->success('中餐新闻查询成功',$list);
    }
    //显示内容
    public function NewsInfo(){
        $id = input('id/d');
        if(empty($id)){
            $this->error('参数错误');
        }
        $news = NewsModel::withoutField('delete_time,list_order')->find($id);
        if(!$news){
            $this->error('新闻不存在');
        }
        $news['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($news['content']));
        $this->success('新闻详情',$news);
    }
}